import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description: leetcode.简单题.特殊数组的特征值
 */

class Solution {
    public static int specialArray(int[] nums) {
        //先排序
        Arrays.sort(nums);
        int length = nums.length;
        //设特征值为x，要满足要求，即 x 的位置之后必须有 x 个数
        //x 必须置于length - x 前一个位置
        if(length == 1) {
            if(nums[0] > 0) {
                return 1;
            } else {
                return -1;
            }
        }
        for(int i = 0; i < nums.length - 1; i++) {
            if(i == 0) {
                if(length - i <= nums[0]) {
                    return length;
                } else {
                    continue;
                }
            }
            if(length - i - 1 != 0 && length - i - 1 > nums[i] && length - i - 1 <= nums[i + 1]) {
                return length - i - 1;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] nums = new int[]{3,6,7,7,0};
        int count = specialArray(nums);
    }
}

public class Test {
}
